import org.bouncycastle.cert.X509CertificateHolder;
import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter;
import org.bouncycastle.openssl.PEMParser;

import java.io.FileReader;
import java.security.PublicKey;
import java.security.cert.X509Certificate;

/**
 * @ClassName CertificateExample
 * @Description TODO
 * @Author 赵雷棚
 * @Date 2024/6/26 14:43
 **/
// 使用证书
public class CertificateExample {

    public static PublicKey loadPublicKeyFromCertificate(String certFilePath) throws Exception {
        PEMParser pemParser = new PEMParser(new FileReader(certFilePath));
        X509CertificateHolder certificateHolder = (X509CertificateHolder) pemParser.readObject();
        JcaX509CertificateConverter converter = new JcaX509CertificateConverter();
        X509Certificate certificate = converter.getCertificate(certificateHolder);
        return certificate.getPublicKey();
    }
}
